package com.prs.crm.service.oa.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.prs.crm.context.AppContext;
import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.oa.OaTaskDao;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.Notice;
import com.prs.crm.domain.oa.OaTask;
import com.prs.crm.domain.oa.OaTaskSubmit;
import com.prs.crm.exception.InValidException;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.impl.BaseServiceImpl;
import com.prs.crm.service.oa.NoticeService;
import com.prs.crm.service.oa.OaTaskService;
import com.prs.crm.service.oa.OaTaskSubmitService;

public class OaTaskServiceImpl extends BaseServiceImpl<OaTask,OaTaskDao> implements OaTaskService{
	private PersonService personService;
	private OaTaskSubmitService oaTaskSubmitService;
	private NoticeService noticeService;

	@Override
	public void saveTask(OaTask task, File file, String fileName,String principals,String copyToPersons) {
		// TODO Auto-generated method stub
		if(task.getBeginTime().compareTo(task.getEndTime()) > 0) {
			throw new InValidException("任务开始时间不能晚于任务结束时间");
		}
		if(task.getUpTask() != null) {
			if(task.getBeginTime().compareTo(task.getUpTask().getBeginTime()) < 0) {
				throw new InValidException("开始时间不能早于父任务开始时间");
			}
			if(task.getEndTime().compareTo(task.getUpTask().getEndTime()) > 0) {
				throw new InValidException("结束时间不能晚于父任务结束时间");
			}
		}
		
		Attach attach = FileUtil.getInstance().saveFile(file, fileName, "", Constrains.UPLOAD_DIR_TASK);
		Person recorder = AppContext.getAccountContext().currentUser();
		if(attach.getSrc() != null) {
			task.setAttach(attach.getSrc());			
		}
		Collection<Person> exePersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(principals.split(",")));
		if(copyToPersons != null) {
			Collection<Person> copyPersons = this.getPersonService().findPersons(ToolKits.convertStringAryToIntAry(copyToPersons.split(",")));
			if(copyPersons != null) {
				task.addCopyToPersons(copyPersons);
				
				for(Person critiquer : copyPersons) {
					String msg = "您得到工作任务：" + task.getTitle() + "的点评！";
					Notice notice = new Notice();
					notice.setTitle("点评工作任务");
					notice.setContent(msg);
					
					this.getNoticeService().sendNotice(notice, critiquer);
				}
			}
		}		
		task.setRecorder(recorder);
		this.getDao().save(task);
		
		List<Person> lstPerson = new ArrayList<Person>();
		lstPerson.addAll(exePersons);
		for(int i=0;i<lstPerson.size();i++) {
			Person person = lstPerson.get(i);
			OaTaskSubmit submit = new OaTaskSubmit();
			submit.setTask(task);
			submit.setExePerson(person);
			submit.setState(Constrains.TASK_EXECUTE);
			this.getOaTaskSubmitService().save(submit);
			
			String msg = "您被分配工作任务：" + task.getTitle();
			Notice notice = new Notice();
			notice.setTitle("执行工作任务");
			notice.setContent(msg);
			
			this.getNoticeService().sendNotice(notice, person);
		}
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setOaTaskSubmitService(OaTaskSubmitService oaTaskSubmitService) {
		this.oaTaskSubmitService = oaTaskSubmitService;
	}

	public OaTaskSubmitService getOaTaskSubmitService() {
		return oaTaskSubmitService;
	}

	@Override
	public Collection<Object[]> getTaskReportDataSouce(String state,
			Date datestart, Date dateend, Collection<Person> persons) {
		// TODO Auto-generated method stub
		return this.getDao().getTaskReportDataSouce(state, datestart, dateend, persons);
	}

	@Override
	public Collection<OaTask> getChildTask(OaTask oaTask) {
		// TODO Auto-generated method stub
		return this.getDao().getChildTask(oaTask);
	}

	@Override
	public Collection<OaTask> getChildTask(OaTask oaTask, Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getChildTask(oaTask, person);
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	@Override
	public Collection<OaTask> getValidCritiqueOaTask(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getValidCritiqueOaTask(person);
	}

	@Override
	public Collection<OaTask> getOverdueOaTask(Person person) {
		// TODO Auto-generated method stub
		return this.getDao().getOverdueOaTask(person);
	}

	@Override
	public List<OaTask> getAllTask() {
		// TODO Auto-generated method stub
		return this.getDao().getAllTask();
		
	}

}
